<?php
/**
 * 常用地址管理
 * @author hushuai
 *
 */
class AddressAction extends CommonAction {
	
	/**
	 * 定义数据表信息
	 */
	private $tableAddress  = "Site";              //定义地址表
	private $Address       = "Address";           //定义地址表
	private $AddMemberView = "AddMemberView";     //定义地址表
	
	/**
	 * 定义提示信息
	 */
	private $addr_error_msg  = "登陆之后可以查看到您的地址列表！";              //定义地址表
	
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    /**
     * 查询地址（分页）
     */
    public function index(){
    	 
    	$desc     = $this->_get("orderDirection", "trim",   "desc");;
    	$order    = $this->_get("orderName",      "trim",   "addtime");
    	$pageNum  = $this->_get("pageNum",        "intval", C('PAGE_NUMS'));
    	$nowPage  = $this->_get("p",              "intval", 1);
    	$where    = $_POST['where'];
    	if($where){
    		foreach ($where as $k=>$v){
    			if($v){
    				$map[$k] = $v;
    			}
    		}
    	}
    	$map['isdel'] = 2 ;
    	$con      = $order.' '.$desc;
    	$table    = $this->tableAddress; 
		
    	$this->success(parent::pageSelect($table,$map,$pageNum,$nowPage,$con));
    }
    

    /**
     * 添加地址
     */
    public function add(){
    	$param = array(
    		"before"  => "address_before_deal",
    		"after"   => "address_after_deal",
    	);
    	parent::do_add($this->tableAddress,$param);
    
    }
    /**
     * 添加用户地址
     */
    public function front_add(){
    	
    	parent::do_add($this->Address);
    
    }
    /**
     * 修改地址
     */
    public function front_edit(){

    	parent::do_edit($this->Address);
    
    }
    /**
     * 修改地址
     */
    public function edit(){
    	$param = array(
    		"before" => "address_before_deal",
    		"after"  => "address_after_deal",
    	);
    	parent::do_edit($this->tableAddress,$param);
    
    }
    
    /**
     * 查询餐厅地址关联详情
     */
     public function detail(){
    
    	$id = $this->_req("id","intval",0);
    	$data  = M($this->Address)->find($id);
    	$this->success($data);
    } 
    
    /**
     * 删除地址
     */
    public function del(){
    
    	$id   = $this->_get("id","intval",0);
    	$maps = array("id"=>$id,"isdel"=>1);
    	$data = M($this->Address)->save($maps);
    	$this->success($data);
    }
    
    /**
     * 查询用户地址列表
     */
    public function byUser(){
    	 
    	$userid = $this->_get("userid","intval",0);
    	$map    = array("isdel"=>2,"userid"=>$userid);
    	$data   = M($this->Address)->where($map)->order("id desc")->limit(5)->select();
    	$this->success($data);
    }
    
    /**
     * 地址无分页查询
     */
    public function select(){
    	
    	$keywords = $this->_req("keywords","trim","");
    	
    	$map['enkeywords'] = array("like","%".$keywords."%");
    	$map['isdel']    = 2;
    	
    	$data = M($this->tableAddress)->where($map)->select();
    	$this->success($data);
    }
    
    /**
     * 添加备注信息点
     */
    public function outletLbs(){
    	 
    	$id      = $this->_req("id","intval",0);
    	$address = $this->_req("address","trim",0);
    	$array   = array();
    	 
    	if($id){
    		$model        = M("Site");
    		$array['row'] = $model->find($id);
    	}else{
    		if(empty($address)){
    			$address = C("MAP_DEFAULT_ADDR");
    		}
    		$array['address'] = $address;
    	}
    	$this->success($array);
    }
    
    /**
     * 根据session中的用户信息获取当前用户的地址列表信息
     * @param $userid int
     * @param $type   varchar
     * return array
     */
    public function getAddressByUser(){
    	
    	$userid = $this->_req("id");
    	$type   = $this->_req("type");
    	$city   = $this->_req('city');
    	
    	if($city){
    		$map['city'] = $city;
    	}
    	
    	$map['userid'] = $userid;
    	$map['isdel']  = 2;
    	$data = M($this->Address)->where($map)->order("id desc")->limit(5)->select();
    	
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error($this->addr_error_msg);
    	}
    }
    
    /**
     * 根据地址编号查询地址详情
     * @param $id int
     * return array
     */
    public function addrdetail(){
    	 
    	$id = $this->_req("id");
    	$map['id']    = $id;
    	$map['isdel'] = 2;
    	$data = M($this->Address)->where($map)->find();
    	$this->success($data);
    }
}